(function(KKBus){

    function ReservationController() {
      
      var self = this;
      
      self.validation = ko.observable(false);
      self.errorMessage = ko.observable("Blad");
      self.success = ko.observable(false);
      
       self.history = ko.observableArray();
        
        self.getHistory = function (page)
        {
             self.history.removeAll();
            
            app.request++;
        
            var test = app.Knockout.AuthenticationController.user;
                 var userIdek = test.id();
                 console.log(userIdek);
                 
             var url = 'rest/reservations/history/user/'+userIdek;
             console.log(url);
                                 
           
        
        
        KKBus.Ajax.doGet(url, function(data, textStatus, jqXHR)
        {
      
        var jsData = JSON.parse(data);
        var i = jsData.length;
        
        
            while(i--){
                 self.history.push(new KKBus.ReservationModel(  KKBus.Format.timestampToDate(jsData[i].date),jsData[i].id,jsData[i].idUser)); 
         }
         app.request--;
                  
          if (app.request == 0) {
                    if (app.settings.DEBUG == true) { console.log("Load template from: " +  ReservationController.name); }
                    setTimeout(function() { app.loadTemplate(); }, 1000);
                }
            });
        };
        
        
        
          self.historybook = ko.observableArray();
        
        self.getHistoryBook = function (page)
        {
            self.historybook.removeAll();
            
            app.request++;
        
             var test2 = app.Knockout.AuthenticationController.user;
             var userIdek = test2.id();
             console.log(userIdek);
             var url = 'rest/reservations/user/'+userIdek;
             console.log(url);
             
                                 
           
        
        
        KKBus.Ajax.doGet(url, function(data, textStatus, jqXHR)
        {
      
        var jsData = JSON.parse(data);
        var i = jsData.length;
        
                                                                                    
            while(i--){
                 self.historybook.push(new KKBus.ReservationModel(KKBus.Format.timestampToDate(jsData[i].date), jsData[i].id,jsData[i].idUser)); 
         }
         app.request--;
                  
          if (app.request == 0) {
                    if (app.settings.DEBUG == true) { console.log("Load template from: " +  ReservationController.name); }
                    setTimeout(function() { app.loadTemplate(); }, 1000);
                }
            });
        };
      
       self.deleteHistoryBook = function(id, index) {
     
         self.historybook.remove(self.historybook()[index]);
         var url = 'rest/reservation/'+id;
         KKBus.Ajax.doDelete(url, function(result, textStatus, jqXHR) {
              });   
              };    
              
      
        self.makeReservation= function() {
          
           
        
          var user = app.Knockout.AuthenticationController.user;
          var userID = user.id();
          var url = 'rest/reservation'; 
          var reservation = JSON.stringify({
                    userId: userID,
                    busStopFirstId: 53,
                    busStopLastId: 56,
                    hour: $('#formHour').val(),
                    places: $('#formPlace').val(),
                    date: $('#datepicker').val()
          
                 }); 
                
                 console.log()                 
                    
                    KKBus.Ajax.doPost(url, reservation, function(result, textStatus, jqXHR) {
                             if (result == "ADDED") {
                                    self.success(true);
                                    setTimeout(function() { app.loadTemplate(); }, 3000);
                                    $('#busStopFirstId').val(' ');
                                    $('#formHour').val(' ');
                                    $('#formPlace').val(' ');
                                    $('#datepicker').val(' ');
                                    $('#busStopFirstId2').val(' ');
                                    $('#formHour2').val(' ');
                                    $('#formPlace2').val(' ');
                                    $('#datepicker2').val(' ');
                                    console.log('dobrze');
                                 }
                            });   
                
        };
        
        
            self.makeReservation2= function() {
          
           
        
          var user = app.Knockout.AuthenticationController.user;
          var userID = user.id();
          var url = 'rest/reservation'; 
          var reservation = JSON.stringify({
                    userId: userID,
                    busStopFirstId: 53,
                    busStopLastId: 56,
                    hour: $('#formHour2').val(),
                    places: $('#formPlace2').val(),
                    date: $('#datepicker2').val()
          
                 }); 
                
                 console.log()                 
                    
                    KKBus.Ajax.doPost(url, reservation, function(result, textStatus, jqXHR) {
                             if (result == "ADDED") {
                                    self.success(true);
                                    setTimeout(function() { app.loadTemplate(); }, 3000);
                                    $('#busStopFirstId').val(' ');
                                    $('#busStopFirstId2').val(' ');
                                    $('#formHour2').val(' ');
                                    $('#formPlace2').val(' ');
                                    $('#datepicker2').val(' ');
                                    console.log('dobrze');
                                 }
                            });   
                
        };
    };
   
    KKBus.ReservationController =  ReservationController;
    
}(window.KKBus));


